/*
 * MODULE  NAME  :
 * PROGRAM NAME  : sbutil.h
 * AUTHOR        : HOTMOCHA
 * CREATE  DATE  : 2015-04-08 22:52:57
 * PROGRAM DESC  :
 *
 * HISTORY       :
 *
 */

#ifndef _H_SBUTIL_H_
#define _H_SBUTIL_H_
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/socket.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>

#define SB_MSG_QUEUE_MAX_MSG_LEN	512
#define SB_NO_WAIT			1
#define SB_WAIT				0

struct mymsgbuf
{
	long mtype;
	char mtext[SB_MSG_QUEUE_MAX_MSG_LEN];
};

struct dns_msg
{
	int mtext_len;
	struct mymsgbuf m;
};


int msg_create ();
int msg_send (int id, struct dns_msg *msg, int nowaitflag);
int msg_recv (int id, long msgtyp, struct dns_msg *msg, int nowaitflag);


int shm_create(int shm_size);
void* shm_attach(int shmid);
int shm_detach(const void *shmaddr);
int shm_ctl(int shmid, struct shmid_ds *buf);


#define DEBUG   1
#define INFO    2
#define ERROR   3
#define FATAL   4
#define SYSTEM  5
#define SB_LOG_BUF_LEN  2048

extern char sb_log_buf[SB_LOG_BUF_LEN + 1];
extern void GenerateLogMsg(char *filename, int lineno, int log_level);
extern int sb_log_level;
extern FILE *sb_log_file;

FILE* SetLogFile(FILE *file);
int SetLogLevel(int level);

#define SystemInfo(format, args... )                                                     \
        do {                                                                            \
                int __len__ = 0;                                                        \
                GenerateLogMsg(__FILE__, __LINE__, SYSTEM);                             \
                __len__ = strlen(sb_log_buf);                                           \
                snprintf(sb_log_buf + __len__, SB_LOG_BUF_LEN - __len__, format, ##args); \
                fprintf(sb_log_file, "%s\n", sb_log_buf);                                        \
                if (sb_log_level == DEBUG)                                              \
                        fprintf(stderr, "%s\n", sb_log_buf);                            \
        } while(0)

#define DebugOutput(format, args...)                                            	\
        do {                                                                            \
                if (sb_log_level <= DEBUG) {                                            \
                        int __len__ = 0;                                                \
                        GenerateLogMsg(__FILE__, __LINE__, DEBUG);                      \
                        __len__ = strlen(sb_log_buf);                                   \
                        snprintf(sb_log_buf + __len__, SB_LOG_BUF_LEN - __len__, format, ##args); \
                        fprintf(sb_log_file, "%s\n", sb_log_buf);            			\
                        fprintf(stderr, "%s\n", sb_log_buf);                            \
                }                                                                       \
        } while(0)

#define InfoOutput(format , args... )                                                   \
        do {                                                                            \
                if (sb_log_level <= INFO) {                                             \
                        int __len__ = 0;                                                \
                        GenerateLogMsg(__FILE__, __LINE__, INFO);                       \
                        __len__ = strlen(sb_log_buf);                                   \
                        snprintf(sb_log_buf + __len__, SB_LOG_BUF_LEN - __len__, format, ##args); \
                        fprintf(sb_log_file, "%s\n", sb_log_buf);            			\
                        if (sb_log_level == DEBUG)                                      \
                                fprintf(stderr, "%s\n", sb_log_buf);                    \
                }                                                                       \
        } while(0)

#define ErrorOutput(format , args... )                                                  \
        do {                                                                            \
                if (sb_log_level <= ERROR) {                                            \
                        int __len__ = 0;                                                \
                        GenerateLogMsg(__FILE__, __LINE__, ERROR);                      \
                        __len__ = strlen(sb_log_buf);                                   \
                        snprintf(sb_log_buf + __len__, SB_LOG_BUF_LEN - __len__, format, ##args); \
                        fprintf(sb_log_file, "%s\n", sb_log_buf);            			\
                        if (sb_log_level == DEBUG)                                      \
                                fprintf(stderr, "%s\n", sb_log_buf);                    \
                }                                                                       \
        } while(0)

#define FatalOutput(format , args... )                                                  \
        do {                                                                            \
                int __len__ = 0;                                                        \
                GenerateLogMsg(__FILE__, __LINE__, FATAL);                              \
                __len__ = strlen(sb_log_buf);                                           \
                snprintf(sb_log_buf + __len__, SB_LOG_BUF_LEN - __len__, format, ##args); \
                fprintf(sb_log_file, "%s\n", sb_log_buf);                    			\
                if (sb_log_level == DEBUG)                                              \
			fprintf(stderr, "%s\n", sb_log_buf);                            \
        } while(0)

int sb_set_tcpkeepalive(int s);
int sb_set_nonblocking(int s);
int sb_set_tcppush(int s, int onoff);
int sb_set_reuseaddr(int s, int onoff);

int sb_get_cpu_num();

int sb_save_argv_env(int argc, char **argv);
int sb_setproctitle(char *title);
void sb_set_process_title(char *fmt,...);

void sb_update_time(struct timeval *cache_time,  char *cache_str_time);

int sb_cmp_sockaddr(struct sockaddr *sa, struct sockaddr *sb);
#endif
